/**
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-10-21     xu           the first version
 */
#include "net_thread_init.h"
#include <rtthread.h>

#define WEATHER_QUERY_INTERVAL (10 * 60 * 1000)

static rt_base_t g_last_query_tick = 0;
static amap_weather_info_t g_last_weather_info = RT_NULL;

static void net_thread_work(void* param)
{
    rt_kprintf("wait for network ready!\n");
    rt_thread_mdelay(15 * 1000);

    while (1) {
        rt_kprintf("request live weather ...\n");
        amap_weather_info_t wea_info = amap_weather_get(LIVE_WEATHER);
        if (wea_info) {
            rt_kprintf("request live weather success!\n");
            if (g_last_weather_info != RT_NULL) {
                amap_weather_delete(g_last_weather_info);
            }
            g_last_weather_info = wea_info;
        }
        rt_thread_mdelay(WEATHER_QUERY_INTERVAL);
    }
}

int net_thread_init()
{
    rt_thread_t thread = rt_thread_create("net", net_thread_work, RT_NULL, 4096, 20, 10);
    RT_ASSERT(thread != RT_NULL);
    rt_kprintf("create net thread done!\n");

    RT_ASSERT(rt_thread_startup(thread) == RT_EOK);
    rt_kprintf("start net thread done!\n");
    return 0;
}
INIT_APP_EXPORT(net_thread_init);

amap_weather_info_t net_weather_info_get()
{
    return g_last_weather_info;
}